package com.dyz.leetcode._12_linkedList;

import com.dyz.leetcode.ListNode;

public class _725_splitListToParts {
    public ListNode[] splitListToParts(ListNode head, int k) {
        int len = 0;
        ListNode curr = head;
        while (curr!=null){
            len++;
            curr = curr.next;
        }

        int with = len/k;
        int remainder = len%k;

        ListNode[] res= new ListNode[k];
        curr = head;
        for(int i=0; i<k;i++){
            ListNode dummy = new ListNode(-1);
            ListNode node = dummy;
            int realWith = with +(i<remainder?1:0);
            for(int j=0; j<realWith; j++){
                node.next = new ListNode(curr.val);
                node = node.next;
                if(curr!=null){curr = curr.next;}
            }
            res[i] = dummy.next;
        }
        return res;

    }
}
